<!-- 车型管理 -->
<template>
  <div class="dashboard-container car-models customer-list-box">
    <div class="form">
      <el-form
        :model="searchForm"
        class="elform"
        :inline="true"
      >
        <el-form-item
          label="车型编号:"
          label-width="100px"
        >
          <el-input
            v-model="searchForm.id"
            placeholder="请输入车型编号"
            style="width:275px"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="应载重量:"
          label-width="100px"
        >
          <template>
            <el-select
              v-model="searchForm.allowableLoad"
              placeholder="请选择应载重量"
              style="width:275px"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item
          label="应载体积："
          label-width="100px"
        >
          <template>
            <el-select
              v-model="searchForm.allowableVolume"
              placeholder="请选择应载体积"
              style="width:275px"
            >
              <el-option
                v-for="item in options2"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item
          label="车辆类型："
          label-width="100px"
        >
          <el-input
            v-model="searchForm.name"
            placeholder="请输入车辆类型"
            style="width:275px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="search"
            type="warning"
            @click.prevent="search"
          >搜索</el-button>
          <el-button
            plain
            class="reset"
            @click="reset"
          >重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据渲染 -->
      <div class="eltable">
        <el-button
          class="btn"
          type="warning"
          @click="open"
        >新增车型</el-button>
        <el-table
          v-loading="loading"
          :data="list"
          style="width: 100%"
          :stripe="true"
          class="table"
        >
          <!-- stripe  斑马线 -->
          <el-table-column
            label="序号"
            type="index"
            :index="indexMethod"
            width="60"
          >
          </el-table-column>
          <el-table-column
            label="车型编号"
            prop="id"
            width="160"
          >
          </el-table-column>
          <el-table-column
            label="车辆类型"
            prop="name"
            width="120"
          >
          </el-table-column>
          <el-table-column
            label="车辆数量"
            prop="num"
            width="80"
          >
          </el-table-column>
          <el-table-column
            label="应载重量（吨）"
            prop="allowableLoad"
            width="120"
          >
          </el-table-column>
          <el-table-column
            label="应载体积（立方米）"
            prop="allowableVolume"
            width="150"
          >
          </el-table-column>
          <el-table-column
            label="长（米）"
            prop="measureLong"
            width="120"
          >
          </el-table-column>
          <el-table-column
            label="宽（米）"
            prop="measureWidth"
            width="120"
          >
          </el-table-column>
          <el-table-column
            label="高（米）"
            prop="measureHigh"
            width="120"
          >
          </el-table-column>
          <el-table-column
            label="操作"
            width="100"
          >
            <template #default="{ row }">
              <el-link
                type="primary"
                @click="editDialog(row.id)"
              >编辑</el-link>
              |
              <el-link
                type="danger"
                @click="del(row.id)"
              >删除</el-link>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="pagination"
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="formData.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="formData.pageSize"
          :total="counts"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
        <diaLog
          ref="add"
          :show-dialog.sync="showDialog"
          :title="title"
        ></diaLog>
      </div>
    </div>
  </div>
</template>
<script>
import { delApi, editListRenderApi, pageApi } from '@/api/transit'
import diaLog from '@/views/transit/diaLog.vue'
export default {
  components: {
    diaLog
  },
  data() {
    return {
      searchForm: {
        allowableLoad: '',
        allowableVolume: '',
        id: '',
        name: '',
        page: 1,
        pageSize: 10
      },
      options: [{
        value: 'RANGE_LEVEL_1',
        label: '<1.8（吨）'
      },
      {
        value: 'RANGE_LEVEL_2',
        label: '1.8-6（吨）'
      },
      {
        value: 'RANGE_LEVEL_3',
        label: '6-14（吨）'
      },
      {
        value: 'RANGE_LEVEL_4',
        label: '14-30（吨）'
      },
      {
        value: 'RANGE_LEVEL_5',
        label: '30-50（吨）'
      },
      {
        value: 'RANGE_LEVEL_6',
        label: '50-100（吨）'
      },
      {
        value: 'RANGE_LEVEL_7',
        label: '100>（吨）'
      }],
      options2: [
        {
          value: 'RANGE_LEVEL_1',
          label: '<3（m³）'
        },
        {
          value: 'RANGE_LEVEL_2',
          label: '3-5（m³）'
        },
        {
          value: 'RANGE_LEVEL_3',
          label: '5-10（m³）'
        },
        {
          value: 'RANGE_LEVEL_4',
          label: '10-15（m³）'
        },
        {
          value: 'RANGE_LEVEL_5',
          label: '15-30（m³）'
        },
        {
          value: 'RANGE_LEVEL_6',
          label: '30-50（m³）'
        },
        {
          value: 'RANGE_LEVEL_7',
          label: '50-80（m³）'
        },
        {
          value: 'RANGE_LEVEL_8',
          label: '80-150（m³）'
        },
        {
          value: 'RANGE_LEVEL_9',
          label: '150>（m³）'
        }
      ],
      formData: {
        page: 1,
        pageSize: 10
      },
      list: [],
      counts: 0,
      showDialog: false,
      title: '',
      loading: false
    }
  },
  created() {
    this.render()
  },
  methods: {
    indexMethod(row) {
      return (this.formData.page - 1) * this.formData.pageSize + row + 1
    },
    reset() {
      this.render()
      this.searchForm = {
        allowableLoad: '',
        allowableVolume: '',
        id: '',
        name: ''
      }
    },
    async search() {
      if (this.searchForm.allowableLoad === '' && this.searchForm.allowableVolume === '' && this.searchForm.id === '' && this.searchForm.name === '') return
      const res = await pageApi(this.searchForm)
      console.log(res)
      this.list = res.data.items
      // this.searchForm = {
      //   allowableLoad: '',
      //   allowableVolume: '',
      //   id: '',
      //   name: ''
      // }
    },
    // 打开编辑弹窗
    async editDialog(id) {
      this.showDialog = true
      this.title = '编辑车型'
      const res = await editListRenderApi(id)
      console.log(res)
      this.$refs.add.form = res.data
    },
    open() {
      this.showDialog = true
      this.title = '新增车型'
    },

    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await delApi(id)
        if (this.formData.page > 1 && this.list.length === 1) {
          this.formData.page--
        }
        this.render()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.formData.pageSize = val
      this.render()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.formData.page = val
      this.render()
    },
    async render() {
      this.loading = true
      const res = await pageApi(this.formData)
      // console.log(res)
      this.list = res.data.items
      this.counts = +res.data.counts
      this.loading = false
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>

.search{
  margin-left: 30px;
  margin-right: 20px;
}
.pagination{
  margin-left: 30%;
  color: black;
}
.edit {
  color: #419eff;
}
.del {
  color: red;
}
.form {
  padding-top: 10px;
}
.elform {
  margin: 10px 20px 20px 20px;
  padding-top: 20px;
  background-color: #fff;
}
.eltable {
  margin: 20px;
  padding-bottom: 50px;
  background-color: #fff;
}
.table {
  margin-top: 10px;
  margin-left: 20px;
}
.btn {
  margin-left: 20px;
  margin-top: 20px;
  margin-bottom: 7px;
}
.add-btn {
  margin-bottom: 20px;
}
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
  padding-bottom: 0px;
}

.el-select {
  width: 100%;
}

.car-models /deep/ .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}
.car-models {
  /deep/ .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }
  /deep/ .el-dialog__body {
    text-align: center;
    padding: 40px 60px 0 30px;
  }
  .warn-img {
    width: 40px;
  }
  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }
}
</style>
